package com.leetcode.Offer;

import java.util.ArrayList;

public class JZ54 {
    ArrayList<Integer> list = new ArrayList<>();
    int k;

    public int kthLargest(TreeNode root, int k) {
        this.k = k;
        middleOrder(root);
        return list.get(list.size() - 1);
    }

/*    private void middleOrder(TreeNode root) {
        if (root.left != null) {
            middleOrder(root.left);
        }
        list.add(root.val);
        if (root.right != null) {
            middleOrder(root.right);
        }
    }*/

    private void middleOrder(TreeNode root) {
        if (root.right != null) {
            middleOrder(root.right);
        }
        if (list.size() == k) {
            return;
        }
        list.add(root.val);
        if (root.left != null) {
            middleOrder(root.left);
        }
    }
}
